package com.xk857.article.mapper.provider;


import com.xk857.article.req.LabelREQ;
import org.apache.commons.lang.StringUtils;

/**
 * @author CV大魔王
 * @Description 知识点拼接SQL语句类
 * @date 2021/2/22 9:18
 */
public class LabelProvider {

    /**
     * 条件查询
     *
     * @return
     */
    public String queryPage(LabelREQ req) {
        StringBuilder sql = new StringBuilder();
        // 拼接查询字段
        sql.append("SELECT xl.id, xl.category_id, xl.`name`, xl.remark, xl.title_img, xl.sort, xl.create_date, xl.update_date, xc.`name` c_name ");
        // 拼接表的查询条件
        sql.append(" FROM xk857_label xl JOIN xk857_category xc ON xl.category_id = xc.id WHERE 1=1 ");
        // 拼接条件
        if (StringUtils.isNotEmpty(req.getName())) {
            sql.append(" AND xl.`name` LIKE '%").append(req.getName()).append("%' ");
        }
        if (StringUtils.isNotEmpty(req.getCategoryId())) {
            sql.append(" AND xl.category_id = '").append(req.getCategoryId()).append("' ");
        }
        if (StringUtils.isNotEmpty(req.getCategoryName())) {
            sql.append(" AND xc.`name` = '").append(req.getCategoryName()).append("' ");
        }
        // 拼接排序 先根据排序升序，然后根据更新时间降序
        sql.append(" ORDER BY xl.sort AND xl.update_date DESC");
        return sql.toString();
    }

}
